home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_07 / allison / ddir3.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-02  |  3.4 KB  |  151 lines

  1. LISTING 5 - Derives Exception Classes from the Standard Exception
  2. Hierarchy
  3.  
  4. // ddir3.cpp
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <io.h>
  8. #include <string.h>
  9. #include <sys/stat.h>
  10. #include <dirent.h>
  11. #include <dir.h>
  12. #include <iostream.h>
  13. #include <fstream.h>
  14. #include <strstream.h>
  15. #include <cstring.h>
  16. #include <except.h>
  17.  
  18. // Exception classes
  19. class Dir_err : public runtime
  20. {
  21. public:
  22.     Dir_err(const string& s) : runtime(s){}
  23. };
  24.  
  25. class Bad_dir : public Dir_err
  26. {
  27. public:
  28.     Bad_dir(const string& s) : Dir_err(s){}
  29. };
  30.  
  31. class Dir_open_err : public Dir_err
  32. {
  33. public:
  34.     Dir_open_err(const string& s) : Dir_err(s){}
  35. };
  36.  
  37. class File_del_err : public Dir_err
  38. {
  39. public:
  40.     File_del_err(const string& s) : Dir_err(s){}
  41. };
  42.  
  43. class Dir_del_err : public Dir_err
  44. {
  45. public:
  46.     Dir_del_err(const string& s) : Dir_err(s){}
  47. };
  48.  
  49. // Put response file in root directory
  50. char response_file[L_tmpnam+1] = "\\";
  51. // (Change this to "/" for UNIX)
  52.  
  53.  
  54. main(int argc, char **argv)
  55. {
  56.     char *old_path = getcwd(NULL,MAXDIR);
  57.     void rd(char *);
  58.  
  59.     // Initialize response file for DOS "del" command
  60.     tmpnam(response_file+1);
  61.     ofstream(response_file) << "Y\n";
  62.  
  63.     // Delete the directories
  64.     while (--argc)
  65.     {
  66.         try
  67.         {
  68.             rd(*++argv);
  69.         }
  70.  
  71.         // Catch exceptions
  72.         catch(const Bad_dir& x)
  73.         {
  74.             cerr << "Invalid directory: "
  75.                  << x.what() << endl;
  76.         }
  77.         catch(const Dir_open_err& x)
  78.         {
  79.             cerr << "Error opening directory: "
  80.                  << x.what() << endl;
  81.         }
  82.         catch(const File_del_err& x)
  83.         {
  84.             cerr << "Error deleting file: "
  85.                  << x.what() << endl;
  86.         }
  87.         catch(const Dir_del_err& x)
  88.         {
  89.             cerr << "Error deleting directory: "
  90.                  << x.what() << endl;
  91.         }
  92.         catch(const Dir_err& x)
  93.         {
  94.             cerr << "Directory error: "
  95.                  << x.what() << endl;
  96.         }
  97.     }
  98.  
  99.     // Clean-up
  100.     remove(response_file);
  101.     chdir(old_path);
  102.     return 0;
  103. }
  104.  
  105. void rd(char* dir)
  106. {
  107.     // Log onto the directory that is to be deleted
  108.     if (chdir(dir))
  109.         throw Bad_dir(dir);
  110.     cout << strlwr(dir) << endl;
  111.  
  112.     // Delete all normal files via OS shell
  113.     const size_t SH_CMD_LEN = 14;
  114.     char sh_cmd[L_tmpnam+SH_CMD_LEN+1];
  115.     ostrstream(sh_cmd,sizeof sh_cmd) << "del *.* <"
  116.                                      << response_file
  117.                                      << " >nul"
  118.                                      << ends;
  119.     system(sh_cmd);
  120.  
  121.     // Delete any remaining directory entries
  122.     DIR *dirp;
  123.     struct dirent *entry;
  124.     if ((dirp = opendir(".")) == NULL)
  125.         throw Dir_open_err(dir);
  126.     while ((entry = readdir(dirp)) != NULL)
  127.     {
  128.         struct stat finfo;
  129.  
  130.         if (entry->d_name[0] == '.')
  131.             continue;
  132.         stat(entry->d_name,&finfo);
  133.         if (finfo.st_mode & S_IFDIR)
  134.             rd(entry->d_name);      // Subdirectory
  135.         else
  136.         {
  137.             // Enable delete of file, then do it
  138.             chmod(entry->d_name,S_IWRITE);
  139.             if (unlink(entry->d_name))
  140.                 throw File_del_err(entry->d_name);
  141.         }
  142.     }
  143.     closedir(dirp);
  144.  
  145.     // Remove the directory from its parent
  146.     chdir("..");
  147.     if (rmdir(dir))
  148.         throw Dir_del_err(dir);
  149. }
  150.  
  151.